import { useUserStore } from "@/stores";
import { createRouter, createWebHistory } from "@ionic/vue-router";
import { RouteRecordRaw } from "vue-router";

const routes: Array<RouteRecordRaw> = [
  {
    path: "/login",
    name: "login",
    component: () => import("@/views/login/loginPage.vue"),
  }, //登录页
  {
    path: "/register",
    name: "register",
    component: () => import("@/views/login/registerPage.vue"),
  }, //注册页
  {
    path: "/",
    redirect: "/home",
  },
  {
    path: "/home",
    name: "home",
    component: () => import("@/views/home/attendancePage.vue"),
  },
];

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes,
});

router.beforeEach((to) => {
  // 如果没有token, 且访问的是非登录页，拦截到登录，其他情况正常放行
  const useStore = useUserStore();
  if (!useStore.token && to.path !== "/login" && to.path !== "/register")
    return "/login";
});

export default router;
